﻿using System;
using System.Web;
using NHibernate;
using SimpleServiceBus.Persistence.NHibernate;

namespace SimpleServiceBus.EndpointMonitor.Web
{
    public class WebSession : ISessionStorage
    {
        private const string WebSessionKey = "ssb_nh_websesion";

        public ISession Session
        {
            get
            {
                if (HttpContext.Current != null)
                    return HttpContext.Current.Items[WebSessionKey] as ISession;
                return null;
            }

            set
            {
                if (HttpContext.Current != null)
                    HttpContext.Current.Items[WebSessionKey] = value;
                else
                {
                    throw new InvalidOperationException(
                        "HttpContext.Current is null - WebSession requires a valid HttpContext.");
                }
            }
        }
    }
}
